package com.xu.sms.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtil {
	
	static Map<Integer,String> weekMaps = new HashMap<Integer, String>();
	
	static{
		weekMaps.put(2, "星期一");
		weekMaps.put(3, "星期二");
		weekMaps.put(4, "星期三");
		weekMaps.put(5, "星期四");
		weekMaps.put(6, "星期五");
		weekMaps.put(7, "星期六");
		weekMaps.put(1, "星期日");
	}
	
	
	public static Date strChangeDate(String dateStr){
		System.out.println(dateStr);
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static String formatDate(Date date,String dateFromat){
		SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);
		return sdf.format(date);
	}
	
	//计算两个日期的时间差
	public static int dateAndDateDiff(Date date1,Date date2){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(date1);
		 int date1_day = cal.get(Calendar.DAY_OF_YEAR);
		 cal.setTime(date2);
		 int date2_day = cal.get(Calendar.DAY_OF_YEAR);
		 int result = date1_day - date2_day;
		return Math.abs(result);
	}
	
	//根据指定日期来返回该日期对应的星期天
	public static String getDateWeek(Date date){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
		 int week = cal.get(Calendar.DAY_OF_WEEK);
		return weekMaps.get(week);
	}

}
